# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

PREF_JS_EXPORTS = $(srcdir)/profile/komodo.js \
		  $(NULL)

# hardcode en-US for the moment
AB_CD = en-US

DEFINES += \
  -DAB_CD=$(AB_CD) \
  -DKOMODO_ICO='"$(srcdir)/komodo.ico"' \
  -DDOCUMENT_ICO='"$(DIST)/branding/document.ico"' \
  $(NULL)

# Build a binary bootstrapping with XRE_main

ifndef MOZ_WINCONSOLE
ifdef MOZ_DEBUG
MOZ_WINCONSOLE = 1
else
MOZ_WINCONSOLE = 0
endif
endif

# This switches $(INSTALL) to copy mode, like $(SYSINSTALL), so things that
# shouldn't get 755 perms need $(IFLAGS1) for either way of calling nsinstall.
NSDISTMODE = copy

include $(topsrcdir)/config/config.mk

PROGRAMS_DEST = $(DIST)/bin

include $(topsrcdir)/config/rules.mk

ifeq ($(MOZ_WIDGET_TOOLKIT),gtk2)
# Ensure komodo binary uses LD_LIBRAY_PATH of the binary's directory.
LDFLAGS += -Wl,-rpath=\$$ORIGIN
endif

ifneq (,$(filter-out OS2 WINNT,$(OS_ARCH)))

GARBAGE += $(addprefix $(DIST)/bin/defaults/pref/, komodo.js)

endif

ifeq ($(MOZ_WIDGET_TOOLKIT),gtk2)
libs::
	$(INSTALL) $(IFLAGS1) $(srcdir)/default.xpm    $(DIST)/bin/chrome/icons/default
endif

libs:: $(srcdir)/profile/prefs.js
	$(INSTALL) $(IFLAGS1) $^ $(DIST)/bin/defaults/profile

ifeq (cocoa,$(MOZ_WIDGET_TOOLKIT))

APP_NAME = $(MOZ_APP_DISPLAYNAME)

ifdef MOZ_DEBUG
APP_NAME := $(APP_NAME)Debug
endif

LOWER_APP_NAME = $(shell echo $(APP_NAME) | tr '[A-Z]' '[a-z]')

AB_CD = $(MOZ_UI_LOCALE)

AB := $(firstword $(subst -, ,$(AB_CD)))

clean clobber repackage::
	$(RM) -r $(DIST)/$(APP_NAME).app

APPFILES = MacOS

libs repackage:: $(PROGRAM) application.ini
	$(MKDIR) -p $(DIST)/$(APP_NAME).app/Contents/MacOS
	rsync -a --exclude CVS --exclude "*.in" $(srcdir)/macbuild/Contents $(DIST)/$(APP_NAME).app --exclude English.lproj
	$(MKDIR) -p $(DIST)/$(APP_NAME).app/Contents/Resources/$(AB).lproj
	rsync -a --exclude CVS --exclude "*.in" $(srcdir)/macbuild/Contents/Resources/English.lproj/ $(DIST)/$(APP_NAME).app/Contents/Resources/$(AB).lproj
	sed -e "s/%APP_VERSION%/$(MOZ_APP_VERSION)/" -e "s/%APP_NAME%/$(APP_NAME)/" -e "s/%LOWER_APP_NAME%/$(LOWER_APP_NAME)/" $(srcdir)/macbuild/Contents/Info.plist.in > $(DIST)/$(APP_NAME).app/Contents/Info.plist
	sed -e "s/%APP_NAME%/$(APP_NAME)/" $(srcdir)/macbuild/Contents/Resources/English.lproj/InfoPlist.strings.in | iconv -f UTF-8 -t UTF-16 > $(DIST)/$(APP_NAME).app/Contents/Resources/$(AB).lproj/InfoPlist.strings
	rsync -a $(DIST)/bin/ $(DIST)/$(APP_NAME).app/Contents/$(APPFILES)
	$(RM) $(DIST)/$(APP_NAME).app/Contents/$(APPFILES)/mangle $(DIST)/$(APP_NAME).app/Contents/$(APPFILES)/shlibsign
	$(RM) $(DIST)/$(APP_NAME).app/Contents/MacOS/$(PROGRAM)
	rsync -aL $(PROGRAM) $(DIST)/$(APP_NAME).app/Contents/MacOS
	-cp -L $(DIST)/bin/mangle $(DIST)/bin/shlibsign $(DIST)/$(APP_NAME).app/Contents/$(APPFILES)
	cp -RL $(srcdir)/macbuild/komodo.icns $(DIST)/$(APP_NAME).app/Contents/Resources/komodo.icns
#       remove CVS dirs from packaged app
	find $(DIST)/$(APP_NAME).app -type d -name "CVS" -prune -exec rm -rf {} \;

endif
